package com.mzy.designMode.adapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/5/17 8:19
 * @Version 1.0
 */
public class RMBTest {
    public static void main(String[] args) {

        Rate rate = new RMBrate();
        ImportCompany importCompany = new ImportCompany();
        ExportCompany exportCompany = new ExportCompany();
        rate.add(importCompany);
        rate.add(exportCompany);
        rate.change(5);


    }
}


// 抽象目标：汇率
abstract class Rate {
    protected List<Company> companies = new ArrayList<Company>();

    //增加观察者
    public void add(Company company){
        companies.add(company);
    }

    //删除观察者
    public void remove(Company company){
        companies.remove(company);
    }

    public abstract void change(int number);
}

// 具体目标：人名币汇率
class RMBrate extends Rate{

    @Override
    public void change(int number) {
        for (Company obs:companies
             ) {
            ((Company) obs).response(number);
        }
    }
}

// 抽象观察者：公司
interface Company{
    void response(int number);
}

// 具体观察者1：进口公司
class ImportCompany implements Company{

    @Override
    public void response(int number) {
        if (number > 0) {
            System.out.println("人民币升值" + number + "个基点，利率升");
        }else if (number < 0){
            System.out.println("人民币贬值" + (-number) + "个基点，利率降");
        }
    }
}

// 观察者2：出口公司
class ExportCompany implements Company{

    @Override
    public void response(int number) {
        if (number > 0) {
            System.out.println("人民币升值" + number + "个基点，利率降");
        }else if (number < 0){
            System.out.println("人民币贬值" + (-number) + "个基点，利率升高");
        }
    }
}